<template>
	<div class="student">
		<!--提示-->
    <div class="tip">
      <p>{{ $t('message.authenticationTip') }}</p>
    </div>
    <div class="v-student">
    	<v-student sort="register" :parentData="data" @change-params="changeParams"></v-student>
    </div>
    <div class="next">
      <button type="button" class="btn btn-orange" :disabled="isNextDisabled" @click="next">{{ $t('message.next') }}</button>
    </div>
    <div class="skip">
      <button type="button" class="btn btn-next" @click="skip">{{ $t('message.skip') }}</button>
    </div>
	</div>
</template>

<script>
import VStudent from '@/components/base/VStudent'
import { delCookie, getCookie, setCookie } from '@/util/util'

export default {
  name: 'RegisterStudent',
  components: {
    VStudent
  },
  data () {
    return {
      data: {
        school: '',
        schoolDistrict: '',
        education: '',
        institute: '',
        major: '',
        grade: '',
        studentID: '',
        enrolledDate: '',
        photoSource: 'studentCard',
//        startDate: '',
        endDate: '',
        photo: ''
      },
      isNextDisabled: true,
      username: '',
      password: ''
    }
  },
  methods: {
    changeParams (val) {
//      this.data = val
      if (val === 'no') {
        this.isNextDisabled = true
      } else {
        this.data = val
        this.isNextDisabled = false
      }
    },
    next () {
      let data = {
        school_name: this.data.school,
        school_district: this.data.schoolDistrict,
        education: this.data.education,
        college: this.data.institute,
        major: this.data.major,
        grade: this.data.grade,
        studentID: this.data.studentID,
        entry_date: this.data.enrolledDate,
        student_front_side_url: this.data.photo,
//        student_card_start: this.data.startDate,
        student_card_end: this.data.endDate
      }
      // 判断学生卡还是校网
      if (this.data.photoSource === 'studentCard') {
        data.student_card_type = 1
      } else {
        data.student_card_type = 2
      }
      this.updateIdentity(data)
    },
    // 第三步骤数据提交要执行的函数
    updateIdentity (data) {
      this.$ajax.proveData(data).then((res) => {
        if (res.code === 200 || res.code === 205) {
          this.$router.push({ path: 'emergency' })
          // this.login()
        } else {
        }
      }).catch((err) => {
        this.$store.commit('alert', {
          switch: true
        })
      })
    },
    skip () {
      this.$router.push({ path: 'success' })
      this.login()
    },
    login () {
      // 获取第一步骤的数据
      //      let strUser = window.sessionStorage.getItem('thaiPayMall_registerUser')
      let strUser = getCookie('thaiPayMall_registerUser')
      let registerUser = JSON.parse(strUser)
      this.username = registerUser.username
      this.password = registerUser.password
      this.$ajax.userLogin(this.username, this.password).then((res) => {
        if (res.code === 200) {
          //          window.sessionStorage.clear()
          delCookie('thaiPayMall_registerUser')
          setCookie('username', res.data.user_name)
          setCookie('profile', res.data.head_portrait)
          setCookie('unreadMsg', res.data.unread_message_count)
          this.$store.commit('updateLoginUser', {
            username: res.data.user_name,
            profile: res.data.head_portrait,
            unreadMsg: res.data.unread_message_count
          })
        } else {
        }
      }).catch((err) => {
        this.$store.commit('alert', {
          switch: true
        })
      })
    }
  }
}
</script>

<style scoped>
  .next button:disabled {
    cursor: not-allowed;
    background: #d7d7d7;
  }
.student {
  width: 800px;
  margin: 60px auto 0;
  padding-bottom: 100px;
}

.tip {
  background: #FFFDF4;
  border: 1px solid #FF600A;
  padding: 10px 23px;
  line-height: 33px;
}

.tip > p {
  padding-left: 32px;
  font-size: 14px;
  color: #333;
  background: url("../../assets/images/icon_errorTips.png") no-repeat left 10px;
}

.v-student {
  margin-top: 60px;
}

.next {
  text-align: center;
  margin-top: 100px;
}

.btn {
  width: 376px;
  height: 48px;
  font-size: 16px;
  border-radius: 2px;
  cursor: pointer;
}

.skip {
  margin-top: 24px;
  text-align: center;
}

.btn-next {
  border: 1px solid #d7d7d7;
  background-color: #fff;
  color: #a5a5a5;
}
</style>
